package com.srp.backend.mapper;

import com.srp.backend.entity.User;
import org.apache.ibatis.annotations.*;

import java.util.List;

@Mapper
public interface UserMapper {
    @Insert("insert into user (username, password, name, phone, email, avatar) " +
            "values (#{username}, #{password}, #{name}, #{phone}, #{email}, #{avatar})")
    int insert(User user);

    @Update("update `user` " +
            "set username = #{username}, password = #{password}, name = #{name}, " +
            "phone = #{phone}, email = #{email}, avatar = #{avatar} where id = #{id}")
    void update(User user);

    @Delete("delete from `user` where id = #{id}")
    void delete(Integer id);

    @Select("select * from user where id= #{id}")
    User selectById(Integer id);

    @Select("select * from user where username like concat('%',#{username},'%')" +
            " and name like concat('%',#{name},'%')")
    List<User> selectFuzzily(@Param("username") String username, @Param("name") String name);

    @Select("select * from user where username like concat('%',#{username},'%')" +
            " and name like concat('%',#{name},'%') limit #{skipnum}, #{pagesize}")
    List<User> selectByPage(Integer skipnum, Integer pagesize, String username, String name);

    @Select("select count(id) from user where username like concat('%',#{username},'%')" +
            " and name like concat('%',#{name},'%')")
    Integer selectCountByPage(String username, String name);
    
    /**
     * 根据用户名查询用户（用于登录）
     */
    @Select("SELECT * FROM user WHERE username = #{username}")
    User findByUsername(@Param("username") String username);
}
